import request from "@/services/interceptors";
import { TextRecordType } from "@/types";
import { DownOutlined, VerticalAlignBottomOutlined } from "@ant-design/icons";
import { ProColumns, ProTable } from "@ant-design/pro-components";
import { useRequest } from "ahooks";
import { Button } from "antd";
import { useParams } from "react-router-dom";

interface TestRecordProps {
  studentId: number;
}
const TestRecord = ({ studentId }: TestRecordProps) => {
  const { runAsync: examResultExportList, loading: exportLoading } = useRequest(
    request.sgks.examResultListExportList,
    { manual: true }
  );

  const exportEaxmResult = async () => {
    const res: any = await examResultExportList(
      {
        studentId: +studentId,
      },
      {
        format: "blob",
      }
    );
    const blob = new Blob([res.data]);
    const downloadUrl = window.URL.createObjectURL(blob);
    // 创建 a 标签并触发下载
    const a = document.createElement("a");
    a.href = downloadUrl;
    // 可根据实际情况设置文件名
    a.download = `考试记录${studentId}_${Date.now()}.xlsx`;
    document.body.appendChild(a);
    a.click();
    a.remove();
    window.URL.revokeObjectURL(downloadUrl);
  };

  const columns: ProColumns<TextRecordType>[] = [
    {
      title: "考试名称",
      dataIndex: "examName",
    },
    {
      title: "考试开始时间",
      dataIndex: "realStartTime",
    },
    {
      title: "考试结束时间",
      dataIndex: "realEndTime",
    },
    {
      title: "总分",
      dataIndex: "totalScore",
    },
    {
      title: "客观题得分",
      dataIndex: "",
      children: [
        {
          title: "填空题",
          dataIndex: "fbQuestionScore",
        },
        {
          title: "选择题",
          dataIndex: "mcQuestionScore",
        },
        {
          title: "判断题",
          dataIndex: "tfQuestionScore",
        },
        {
          title: "问答题",
          dataIndex: "qaQuestionScore",
        },
      ],
    },
    {
      title: "操作题得分",
      dataIndex: "",
      children: [
        {
          title: "基本操作题",
          dataIndex: "basicExerciseScore",
        },
        {
          title: "字处理操作题",
          dataIndex: "wordExerciseScore",
        },
        {
          title: "电子表格操作题",
          dataIndex: "excelExerciseScore",
        },
        {
          title: "演示文稿操作题",
          dataIndex: "pptExerciseScore",
        },
      ],
    },
  ];

  return (
    <ProTable<TextRecordType>
      rowKey="key"
      pagination={{
        showQuickJumper: true,
      }}
      columns={columns}
      request={async (params, sort, filter) => {
        console.log(sort, filter);
        const res: any = await request.sgks.examGetStudentExamResultsList({
          studentId: +studentId,
          pageNo: params.current,
          pageSize: params.pageSize,
        });
        return {
          data: res.data.records,
          total: res.data?.totalCount,
        };
      }}
      search={false}
      dateFormatter="string"
      toolBarRender={() => [
        <Button
          type="primary"
          icon={<VerticalAlignBottomOutlined />}
          onClick={exportEaxmResult}
          loading={exportLoading}
          disabled={exportLoading}
        >
          导出
        </Button>,
      ]}
    />
  );
};

export default TestRecord;
